package com.huluobo.high3videoproject.mvp.search.view;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.base.view.BaseFragment;
import com.huluobo.high3videoproject.R;
import com.huluobo.high3videoproject.adapter.SearchAdapter;
import com.huluobo.high3videoproject.bean.VideoBeanResult;
import com.huluobo.high3videoproject.mvp.search.presenter.SearchContract;
import com.huluobo.high3videoproject.mvp.search.presenter.SearchPresenter;

import java.util.ArrayList;
import java.util.List;

public class NearByFragment extends BaseFragment<SearchContract.View, SearchPresenter> implements SearchContract.View {

    private RecyclerView recyclerView;

    private List<VideoBeanResult.VideoBean> videoBeanList = new ArrayList<>();

    private SearchAdapter searchAdapter;

    private ImageView search_iv;
    private EditText search_ed;

    @Override
    public void onGetVideosSuccess(VideoBeanResult videoBeanResult) {
        videoBeanList.addAll(videoBeanResult.getData());
        searchAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetVideosFailed(String error) {
        ToastUtils.showLong(error);
    }

    @Override
    public void onFindVideoSuccess(VideoBeanResult videoBeanResult) {
        videoBeanList.clear();
        videoBeanList.addAll(videoBeanResult.getData());
        searchAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFindVideoFailed(String error) {
        ToastUtils.showLong(error);
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.fragment_nearby;
    }

    @Override
    protected void initData() {
        presenter = new SearchPresenter();
        presenter.getVideos(3, 10);
        search_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (search_ed.getText().length() != 0) {
                    presenter.findVideoByName(search_ed.getText().toString());
                }
            }
        });
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.search_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        searchAdapter = new SearchAdapter(videoBeanList);
        recyclerView.setAdapter(searchAdapter);

        search_ed = findViewById(R.id.search_ed);
        search_iv = findViewById(R.id.search_iv);
    }
}
